package com.tangula.web.kashine.business;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.tangula.web.form.DefaultPagingBody;
import com.tangula.web.kashine.entity.LoadOrgStoreForm;
import com.tangula.web.kashine.entity.PagableForm;
import com.tangula.web.kashine.entity.StoreEntity;
import com.tangula.web.kashine.entity.StoreForm;
import com.tangula.web.kashine.repository.StoreRepository;
@Service
public class StoreBusinessImpl implements StoreBusiness {
	@Autowired
	StoreRepository repo;
	@Override
	public DefaultPagingBody<StoreEntity> loadOrgStore(LoadOrgStoreForm from) {
		if(from!=null) {
			DefaultPagingBody<StoreEntity> d = new DefaultPagingBody<>(from.getIndex(),
					from.getSize(),
					repo.countByOwnerOrganizationAndStatus(from.getCondition(),"y"), 
					repo.findByOwnerOrganizationAndStatus(from.getCondition(),"y",PageRequest.of(from.getIndex()-1, from.getSize(), Sort.by(new Sort.Order(Direction.ASC, "id")))));
		return d;	
		}else {
			return null;
		}
	}	
	@Override
	public StoreEntity addOrgStore(StoreEntity store) {
		if(store!=null) {
		return repo.saveAndFlush(store);
		}else {
			return null;
		}
	}
	@Override
	public StoreEntity removeStore(StoreEntity store) {
		if(store!=null) {
		store.setOwnerOrganization(null);
		return repo.saveAndFlush(store);
		}else {
			return null;
		}
	}
	@Override
	public DefaultPagingBody<StoreEntity> loadStore(PagableForm pag) {
		if(pag!=null) {
			PageRequest paging = PageRequest.of(pag.getIndex()-1, pag.getSize(), Sort.by(new Sort.Order(Direction.ASC, "id")));
			Page<StoreEntity> res = repo.findAll(paging);
			DefaultPagingBody<StoreEntity> d = new DefaultPagingBody<>(res.getNumber(),
					res.getSize(),
					(int)res.getTotalElements(),
					res.getContent());
			return d;
		}else {
			return null;
		}
	}
	@Override
	public StoreEntity seleteStore(StoreForm store) {
		if(store!=null) {
			return repo.findById(store.getCondition()).get();
		}else {
			return null;
		}
	}
	@Override
	public StoreEntity changeStore(StoreEntity store) {
		if(store!=null) {
			return repo.saveAndFlush(store);
		}else {
			return null;
		}
	}
	@Override
	public Boolean deletStore(StoreForm store) {
		if(store!=null) {
			StoreEntity s = repo.findById(store.getCondition()).get();
			s.setStatus("n");
			StoreEntity ss = repo.saveAndFlush(s);
			if(ss!=null) {
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}
	
	@Override
	public boolean block(String storeId) {
		StoreEntity entity = repo.findById(storeId).orElseGet(()->null);
		if(entity!=null) {
			entity.setStatus("n");
			repo.save(entity);
		}
		return true;
	}
	
	@Override
	public boolean unblock(String storeId) {
		StoreEntity entity = repo.findById(storeId).orElseGet(()->null);
		if(entity!=null) {
			entity.setStatus("y");
			repo.save(entity);
		}
		return true;
	}
	@Override
	public boolean updateNameDescIcon(StoreEntity entity) {
		StoreEntity store = repo.findById(entity.getId()).orElseGet(()->null);
		if(store!=null) {
			store.setStoreName(entity.getStoreName());
			store.setStoreDescription(entity.getStoreDescription());
			store.setStoreImage(entity.getStoreImage());
			repo.saveAndFlush(store);
		}
		return false;
	}
	@Override
	public boolean removeStore(String id) {
		repo.deleteById(id);
		return true;
	}
	@Override
	public int countByOrgId(String id) {
		int res = 0;
		
		res = repo.countByOwnerOrganization(id);
		
		return res;
	}
	
}
